package com.xinnet.mms.role.service.impl;

import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xinnet.mms.entity.Resources;
import com.xinnet.mms.entity.Role;
import com.xinnet.mms.entity.RoleResources;
import com.xinnet.mms.resources.dao.ResourcesDao;
import com.xinnet.mms.role.dao.RoleDao;
import com.xinnet.mms.role.service.RoleService;
import com.xinnet.mms.roleresources.dao.RoleResourcesDao;

@Service("roleService")
public class RoleServiceImpl implements RoleService{
	
	@Autowired
	RoleDao roleDao;
	
	@Autowired
	RoleResourcesDao roleResourcesDao;
	
	@Autowired
	ResourcesDao resourcesDao;
	
	public Role getRoleByRoleName(String roleName){
		Role role = roleDao.getRoleByRoleName(roleName);
		return role;
	}
	public List<Role> getRoleListByRoleName(String roleName){
		List<Role> roleliList = roleDao.getRoleListByRoleName(roleName);
		return roleliList;
	}
	
	public Role getRoleById(String roleId){
		Role role = roleDao.getRoleById(roleId);
		List<Resources> resourcesList = roleResourcesDao.getResourcesListByRoleId(roleId);
		role.setResourcesList(resourcesList);
		return role;
	}
	
	public void addRole(String roleName,String resourcesIdStr){
		String[] resourcesIdArray = resourcesIdStr.split(",");
		String roleId = UUID.randomUUID().toString();
		Role role = new Role();
		role.setId(roleId);
		role.setName(roleName);
		roleDao.addRole(role);
		for (int i = 0; i < resourcesIdArray.length; i++) {
			String resourcesId = resourcesIdArray[i];
			RoleResources roleResources =new RoleResources();
			roleResources.setId(roleId);
			roleResources.setDescription(resourcesId);
			roleResources.setResId(resourcesId);
			roleResourcesDao.addRoleResources(roleResources);
		}
	}
	
	public void updateRole(Role role,String resourcesIdStr){
		String roleId = role.getId();
		roleResourcesDao.delRoleResourcesByRoleId(roleId);
		String[] resourcesIdArray = resourcesIdStr.split(",");
		for (int i = 0; i < resourcesIdArray.length; i++) {
			RoleResources roleResources =new RoleResources();
			String resourcesId = resourcesIdArray[i];
			roleResources.setId(roleId);
			roleResources.setResId(resourcesId);
			roleResourcesDao.addRoleResources(roleResources);
		}
		roleDao.updateRole(role);
	}
	
	public String delRoleById(String roleId){
		roleResourcesDao.delRoleResourcesByRoleId(roleId);
		roleDao.delRoleById(roleId);
		return "1";
	}
	
	public List<Role> getRoleList(){
		List<Role> roleList = roleDao.getRoleList();
		return roleList;
	}
	
	
	public String getResourcesTree(){
		List<Resources> resourcesList = resourcesDao.getResourcesList();
		if(resourcesList==null || resourcesList.size()<=0){
			return null;
		}
		StringBuffer sb = new StringBuffer();
		sb.append("[");
		for (int i=0;i<resourcesList.size();i++) {
			
			Resources resources = resourcesList.get(i);
			int id = resources.getId();
			String name = resources.getRemark();
			String parentId = resources.getParentId()+"";
			if(i!=0){
				sb.append(",");
			}
			if("客户管理".equals(name)){
				sb.append("{id:"+id+",pId:"+parentId+",name:\""+name+"\",open:true}");
			}else{
				sb.append("{id:"+id+",pId:"+parentId+",name:\""+name+"\"}");
			}
				
		}
		sb.append("]");
		return sb.toString();
	}
	
	public String getResourcesTreeByRoleId(String roleId){
		List<Resources> resourcesList = resourcesDao.getResourcesList();
		
		
		List<Resources>  usedResourcesList = roleResourcesDao.getResourcesListByRoleId(roleId);


		
		if(resourcesList==null || resourcesList.size()<=0){
			return null;
		}
		StringBuffer sb = new StringBuffer();
		sb.append("[");
		for (int i=0;i<resourcesList.size();i++) {
			
			Resources resources = resourcesList.get(i);
			int id = resources.getId();
			String name = resources.getRemark();
			String parentId = resources.getParentId()+"";
			if(i!=0){
				sb.append(",");
			}
			boolean isUsedTheNode = false;
			if(usedResourcesList!=null && usedResourcesList.size()>0){
				for (Iterator<Resources> iterator = usedResourcesList.iterator(); iterator.hasNext();) {
					Resources tempTesources = (Resources) iterator.next();
					int tempId = tempTesources.getId();
					if(id==tempId){
						isUsedTheNode = true;
						break;
					}
				}
			}
			if(isUsedTheNode){
				if("客户管理".equals(name)){
					sb.append("{id:"+id+",pId:"+parentId+",name:\""+name+"\",checked:true,open:true}");
				}else{
					sb.append("{id:"+id+",pId:"+parentId+",name:\""+name+"\",checked:true}");
				}
				
			}else{
				if("客户管理".equals(name)){
					sb.append("{id:"+id+",pId:"+parentId+",name:\""+name+"\",open:true}");
				}else{
					sb.append("{id:"+id+",pId:"+parentId+",name:\""+name+"\"}");
				}
			}
		}
		sb.append("]");
		return sb.toString();
	}

}
